/*
 * VentanaProgramaAcademico.java
 *
 * Created on 21 de febrero de 2007, 05:18 PM
 */

package everest3.materialBibliografico;

import everest3.Ayuda.MostrarAyuda; 
import java.awt.Frame; 
import java.awt.event.ActionEvent; 
import java.awt.event.FocusEvent; 
import java.awt.event.MouseEvent; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTextField;
import everest3.utils.Fecha;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Vector;
import javax.swing.text.MaskFormatter;
/**
 * Clase que administra la Creaci&oacute;n de Programas Acad&eacute;micos
 * @author JOSE LIBARDO MOLINA GALVIS
 */
public class VentanaProgramaAcademico extends javax.swing.JDialog{
    
    /**
     * Creates new form VentanaProgramaAcademico
     * @param parent 
     * @param modal 
     */
    public VentanaProgramaAcademico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);        
        initComponents();       
        	
       dimensionarPantalla();       
    }
    
    /**
     * 
     * @param parent 
     * @param modal 
     * @param nombreUsuario 
     */
     public VentanaProgramaAcademico(java.awt.Frame parent, boolean modal,String nombreUsuario) {
        super(parent, modal);        
        initComponents();       
       jTUsuario.setText(nombreUsuario);
       dimensionarPantalla();       
    }
    /**
     * Meotod que centra la ventana en el monitor
     */
    	private void dimensionarPantalla()
  	{
  		Dimension pantalla, cuadro;
  		
		pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		cuadro = this.getSize();				
		
		this.setLocation(((pantalla.width - cuadro.width)/2), 
						(pantalla.height - cuadro.height)/2);
	}
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTCodigo = new javax.swing.JTextField();
        jTNombre = new javax.swing.JTextField();
        botonBuscar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        botonCerrar = new javax.swing.JButton();
        jPEncabezado = new javax.swing.JPanel();
        JTFecha = new javax.swing.JTextField();
        jTUsuario = new javax.swing.JTextField();
        jLlogo = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLTitulo = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLEstado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Programas acad\u00e9micos");
        setResizable(false);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Programas acad\u00e9micos"));
        jLabel1.setText("C\u00f3digo");

        jLabel2.setText("Nombre");

        jTCodigo.setDocument(new everest3.utils.LimitadorCaracteres(jTCodigo,5,0));
        jTCodigo.setToolTipText("C\u00f3digo del programa acad\u00e9mico");
        jTCodigo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTCodigoMouseEntered(evt);
            }
        });
        jTCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTCodigoActionPerformed(evt);
            }
        });
        jTCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTCodigoFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTCodigoFocusLost(evt);
            }
        });

        jTNombre.setToolTipText("Nombre del programa acad\u00e9mico");
        jTNombre.setDocument(new everest3.utils.LimitadorCaracteres(jTNombre,100,2));
        jTNombre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTNombreMouseEntered(evt);
            }
        });
        jTNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTNombreActionPerformed(evt);
            }
        });
        jTNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTNombreFocusGained(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botonBuscar.setText("Ayuda");
        botonBuscar.setToolTipText("Visualiza la Ayuda del Sistema");
        botonBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonBuscarMouseEntered(evt);
            }
        });
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });
        botonBuscar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonBuscarFocusGained(evt);
            }
        });

        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guardar programa acad\u00e9mico");
        botonGuardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonGuardarMouseEntered(evt);
            }
        });
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });
        botonGuardar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonGuardarFocusGained(evt);
            }
        });

        botonCerrar.setText("Cerrar");
        botonCerrar.setToolTipText("Cierra esta ventana");
        botonCerrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botonCerrarMouseEntered(evt);
            }
        });
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cerrarActionPerformed(evt);
            }
        });
        botonCerrar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                botonCerrarFocusGained(evt);
            }
        });

        jPEncabezado.setLayout(null);

        jPEncabezado.setBackground(new java.awt.Color(204, 204, 204));
        JTFecha.setEditable(false);
        JTFecha.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        JTFecha.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        JTFecha.setDragEnabled(true);
        JTFecha.setEnabled(false);
        JTFecha.setFocusable(false);
        JTFecha.setText(FechaAct.obtenerFechaCompleta());
        jPEncabezado.add(JTFecha);
        JTFecha.setBounds(350, 10, 160, 20);

        jTUsuario.setEditable(false);
        jTUsuario.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTUsuario.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTUsuario.setDragEnabled(true);
        jTUsuario.setEnabled(false);
        jPEncabezado.add(jTUsuario);
        jTUsuario.setBounds(350, 40, 160, 20);

        jLlogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/logo.JPG")));
        jLlogo.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLlogo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jPEncabezado.add(jLlogo);
        jLlogo.setBounds(0, 0, 85, 64);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel6.setText("Nuevo");
        jPEncabezado.add(jLabel6);
        jLabel6.setBounds(180, 40, 60, 22);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 48));
        jLabel3.setText("P");
        jPEncabezado.add(jLabel3);
        jLabel3.setBounds(90, 0, 32, 40);

        jLTitulo.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLTitulo.setText("rogramas Acad\u00e9micos");
        jPEncabezado.add(jLTitulo);
        jLTitulo.setBounds(115, 20, 200, 22);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/everest3/imagenes/Barra.jpg")));
        jPEncabezado.add(jLabel4);
        jLabel4.setBounds(0, 0, 520, 65);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPEncabezado, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCerrar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addGap(194, 194, 194))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPEncabezado, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonBuscar)
                    .addComponent(botonGuardar))
                .addGap(1, 1, 1)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jLEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-526)/2, (screenSize.height-273)/2, 526, 273);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCerrarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonCerrarMouseEntered
// TODO add your handling code here:
          barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_botonCerrarMouseEntered

    private void botonCerrarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonCerrarFocusGained
// TODO add your handling code here:
          barraEstado("Cierra esta Ventana y Vuelve al Menu Principal");
    }//GEN-LAST:event_botonCerrarFocusGained

    private void botonBuscarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonBuscarMouseEntered
// TODO add your handling code here:
          barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonBuscarMouseEntered

    private void botonBuscarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonBuscarFocusGained
// TODO add your handling code here:
          barraEstado("Visualiza la Ayuda del Sistema");
    }//GEN-LAST:event_botonBuscarFocusGained

    private void botonGuardarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonGuardarMouseEntered
// TODO add your handling code here:
        barraEstado("Guarda el Nuevo Programa Acad\u00e9mico en el Sistema");
    }//GEN-LAST:event_botonGuardarMouseEntered

    private void botonGuardarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_botonGuardarFocusGained
// TODO add your handling code here:
        barraEstado("Guarda el Nuevo Programa Acad\u00e9mico en el Sistema");
    }//GEN-LAST:event_botonGuardarFocusGained

    private void jTNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTNombreFocusGained
// TODO add your handling code here:
         barraEstado("Digita el Nombre del Programa Acad\u00e9mico (Maximo 100 Caracteres)");
    }//GEN-LAST:event_jTNombreFocusGained

    private void jTNombreMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTNombreMouseEntered
// TODO add your handling code here:
         barraEstado("Digita el Nombre del Programa Acad\u00e9mico (Maximo 100 Caracteres)");
    }//GEN-LAST:event_jTNombreMouseEntered

    private void jTCodigoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTCodigoMouseEntered
// TODO add your handling code here:
        barraEstado("Digita el Codigo del Programa Acad\u00e9mico (Maximo 5 Caracteres)");
    }//GEN-LAST:event_jTCodigoMouseEntered

    private void jTCodigoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTCodigoFocusGained
// TODO add your handling code here:
        barraEstado("Digita el Codigo del Programa Acad\u00e9mico (Maximo 5 Caracteres)");
    }//GEN-LAST:event_jTCodigoFocusGained

    private void jTCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTCodigoFocusLost
// TODO add your handling code here:
        eventoCodigo();         
    }//GEN-LAST:event_jTCodigoFocusLost

    private void jTCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTCodigoActionPerformed
// TODO add your handling code here:
          eventoCodigo();
    }//GEN-LAST:event_jTCodigoActionPerformed

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
// TODO add your handling code here:
         new MostrarAyuda();
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void jTNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTNombreActionPerformed
// TODO add your handling code here:
        validaDatos();
    }//GEN-LAST:event_jTNombreActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
// TODO add your handling code here:
        validaDatos();
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void cerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cerrarActionPerformed
           this.dispose();
    }//GEN-LAST:event_cerrarActionPerformed
    
    private void eventoCodigo()
    {
          if (jTCodigo.getText().length()>0){
             if(!consultarProgramas(jTCodigo.getText())){
               jTCodigo.setText("");
               javax.swing.JOptionPane.showMessageDialog(this,"EL programa acad\u00e9mico\n"+datosPrograma.elementAt(1).toString().trim()+"\nya existe","Error",javax.swing.JOptionPane.WARNING_MESSAGE);  
               jTCodigo.requestFocus();
           }
            
        }
        
    }
 private boolean validarCamposVacios() {
        if((jTCodigo.getText().length()==0) || (jTNombre.getText().length()==0))
            return true;      
        
       else return false;
    }
    
 public void validaDatos(){
     if(!validarCamposVacios()){
         if(consultarProgramas(jTCodigo.getText())){
              NuevoPrograma newProgram = new NuevoPrograma();
          newProgram.nuevoPrograma(jTCodigo.getText(),jTNombre.getText().toUpperCase());  
             
         }
         else
             javax.swing.JOptionPane.showMessageDialog(this,"El programa "+datosPrograma.elementAt(1).toString().trim()+"\nya existe en el sistema","Error",0);
                
             
      limpiar();
         
     }
      else{
          javax.swing.JOptionPane.showMessageDialog(this,"El c\u00f3digo y nombre del programa acad\u00e9mico no pueden quedar vacios","error",0);
      }
                
 }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
         try{
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e){}
         
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaProgramaAcademico(new javax.swing.JFrame(),true).setVisible(true);
            }
        });
    }
    
    private void limpiar(){
        jTCodigo.setText("");
        jTNombre.setText("");
        jTCodigo.requestFocus();
    }

    private boolean consultarProgramas(String codigoPrograma) {
         tablasYBasesdeDatos.JDBCAdapter dataBase = new tablasYBasesdeDatos.JDBCAdapter(
                                                "jdbc:postgresql://localhost:5432/everest",
                                                 "org.postgresql.Driver","postgres","root");
         
        datosPrograma=dataBase.ConsultaIndividual("SELECT" +
                                                    " progcodigo, " +
                                                    "prognombre " +
                                                "FROM " +
                                                    "programas " +
                                                "WHERE " +
                                                    "progcodigo ="+codigoPrograma);
        
         try {
                dataBase.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        if(datosPrograma.size()>0)
            return false;
        else
            return true;
       
       
    }

private void barraEstado(String mensaje)
      {
          jLEstado.setText(mensaje);
      }
 
    
   private boolean nuevo=true;
    Vector datosPrograma;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTFecha;
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JLabel jLEstado;
    private javax.swing.JLabel jLTitulo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLlogo;
    private javax.swing.JPanel jPEncabezado;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTCodigo;
    private javax.swing.JTextField jTNombre;
    private javax.swing.JTextField jTUsuario;
    // End of variables declaration//GEN-END:variables
 Fecha FechaAct= new Fecha();
}
